/*
 * ImportSources module for Netbeans.
 *
 * Copyright (c) 2007, Rogue
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * For queries or bugs, you may contact me via e-mail: roguexz [at] gmail [dot] com
 */
package rogue.netbeans.module.importsources.ui;

import java.util.Enumeration;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;


/**
 * A tree node which is represented as a check-box item. Depending upon the
 * selection mode specified (if deepSelection = true), the check-box may select,
 * or de-select all child items underneath it.
 *
 * @author Rogue
 */
public class JCheckBoxTreeNode extends DefaultMutableTreeNode {
    protected boolean selected = false;
    private boolean deepSelection = false;

    /**
     * Creates a new instance of JCheckBoxTreeNode
     *
     * @param data the data to display
     * @param selected specifies whether this component is selected by default or not.
     * @param deepSelection whether or not to impact changes to all child nodes.
     */
    public JCheckBoxTreeNode(String data, boolean selected,
                             boolean deepSelection) {
        super(data);
        this.selected = selected;
        this.deepSelection = deepSelection;
    }

    public String getUserObject() {
        return (String) super.getUserObject();
    }

    public void setDeepSelection(boolean value) {
        this.deepSelection = value;
    }

    public boolean isDeepSelection() {
        return deepSelection;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean value) {
        selected = value;

        if (selected) {
            TreeNode[] nodes = getPath();

            for (int i = nodes.length - 2; i >= 0; i--) {
                if (((JCheckBoxTreeNode) nodes[i]).isSelected()) {
                    break;
                }

                ((JCheckBoxTreeNode) nodes[i]).selected = value;
            }
        } else {
            checkParentSelectionStateValidity(this);
        }

        if (deepSelection && (getChildCount() != 0)) {
            setChildValuesRecursively(this, value);
        }
    }

    private void setChildValuesRecursively(JCheckBoxTreeNode parent,
                                           boolean value) {
        Enumeration enu = parent.children();

        while (enu.hasMoreElements()) {
            JCheckBoxTreeNode node = (JCheckBoxTreeNode) enu.nextElement();
            node.selected = value;

            if (node.getChildCount() != 0) {
                setChildValuesRecursively(node, value);
            }
        }
    }

    private void checkParentSelectionStateValidity(JCheckBoxTreeNode node) {
        TreeNode[] nodes = node.getPath();

        for (int i = nodes.length - 1; i >= 0; i--) {
            JCheckBoxTreeNode cbtn = (JCheckBoxTreeNode) nodes[i];

            if (!cbtn.isSelected()) {
                continue;
            }

            // check for sibling statuses
            Enumeration enu = cbtn.children();

            while (enu.hasMoreElements()) {
                if (((JCheckBoxTreeNode) enu.nextElement()).isSelected()) {
                    return;
                }

                // status validated, get the hell out of here now.
            }

            cbtn.selected = false;
        }
    }

    public void negateSelection() {
        setSelected(!selected);
    }
}
